import utils from '../../utils/socketTask'
import {
  getAddressList
} from '../../request/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude: '22.655510429590194',
    longitude: '114.01968517355755',
    subkey: '4IOBZ-YX2L5-SRVIV-IWADW-TED27-O5FA4',
    scale: true,
    markers: [],
    showLocation: true,
    enable3D: true,
    showCompass: true,
    enableOverLooking: true,
    enableZoom: true,
    enableScroll: true,
    enableRotate: true,
    enableTraffic: true,
    showDialog: false,
    currentMarker: null,
  },
  getAddressListData() {
    getAddressList().then(res => {
      if (res.code == 200) {
        console.log('地址', res)
        const arr = []
        res.data.forEach(v => {
          v.iconPath = '../../images/location.png'
          v.width = 32
          v.height = 32
          arr.push({
            id: Number(v.addressId),
            latitude: Number(v.addressLatitude),
            longitude: Number(v.addressLongitude),
            width: v.width,
            height: v.height,
            iconPath: v.iconPath,
            title: v.addressName
          })
          this.setData({
            markers: arr
          })
        })
      }
    })
  },
  navi() {
    let plugin = requirePlugin('routePlan');
    let key = '4IOBZ-YX2L5-SRVIV-IWADW-TED27-O5FA4'; //使用在腾讯位置服务申请的key
    let referer = 'wx50b5593e81dd937a'; //调用插件的app的名称
    let endPoint = JSON.stringify({ //终点
      'name': this.data.currentMarker.title,
      'latitude': this.data.currentMarker.latitude,
      'longitude': this.data.currentMarker.longitude
    });
    wx.navigateTo({
      url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
    })
  },
  handleMarkerTap(e) {
    const marker = this.data.markers.find(item => item.id == e.markerId)
    marker && this.setData({
      currentMarker: marker,
      showDialog: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    utils.socketTask()
    this.getAddressListData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    const map = wx.createMapContext('map')
    map.moveToLocation()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})